
//引入网络库
import { get, post } from "../../util/util.js"
// 获取全局应用程序实例对象
var app = getApp();

// 创建页面实例对象
Page({
  /**
   * 页面名称  
   */
  name: "mine",
  /**
   * 页面的初始数据
   */

  data: {
    isLogin: false,  
    uid: '',
    showView1: false,
    showView2: false,
    showView3: false,
    showView4: false,
    showView5: false,
    showView6: false,
    showView7: false,
    showView8: false,
    showView9: false,
    showView10: false,
    zcjf:'',
    yqjf:',',
    role:'',
    defaulImg: "http://wolaimage-10029986.image.myqcloud.com/1-2016051421050035278.png"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    
  },

  onLoad: function () {
    var that = this;
    that.setData({
      // isLogin: false,
      headimg: '',
      uid: '',
      nickname: '点击登陆', 
      showView1: false,
      showView2: false,
      showView3: false,
      showView4: false,////
      showView5: true,
      showView6: true,
      showView7: true,
      showView8: true,
      showView9: true,
      showView10: true,
    });

    post("/com/getActivity", { type:1}).then(data => {//1=注册送积分，2=邀请注册送积分
      if (data.code == 0){
        that.setData({
          zcjf: data.data.title
        });
    }
    })
    post("/com/getActivity", { type: 2 }).then(data => {//1=注册送积分，2=邀请注册送积分
      if (data.code == 0) {
        that.setData({
          yqjf: data.data.title
        });
    }
    })
    wx.getStorage({
      key: 'login',
      success: function (res) {
        // 异步接口在success回调才能拿到返回值
        var data = res.data
        that.setData({
          isLogin: true,
          role: data.data.role,
        });
        console.log('登录返回数据getStorage：==' + JSON.stringify(data))
        var role = "/resources/pt_icon.png";
        if (data.data.role == "1") {
          role = "/resources/pt_icon.png";
          that.setData({
            showView1: false,
            showView2: false,
            showView3: false,
            showView4: false,////
            showView5: true,
            showView6: true,
            showView7: true,
            showView8: true,
            showView9: true,
            showView10: true,
          })
        } else if (data.data.role == "2") {
          role = "/resources/yr_icon.png";
          that.setData({
            showView1: true,
            showView2: true,
            showView3: true,
            showView4: false,////
            showView5: true,
            showView6: true,
            showView7: true,
            showView8: true,
            showView9: true,
            showView10: true,
          })

        } else if (data.data.role == "4") {
          role = "/resources/jz_icon.png";
          that.setData({
            showView1: false,
            showView2: false,
            showView3: true,
            showView4: true,
            showView5: true,
            showView6: true,
            showView7: true,
            showView8: true,
            showView9: true,
            showView10: true,
          })
        }
        that.setData({
          headimg: data.data.headimg,
          uid: data.data.uid,
          nickname: data.data.nickname,
          roleImg: role,
        }); 
      },
      fail: function () {
        console.log('读取key1发生错误') 
       
      }
    })
    // //测试 开发完毕注释掉
    // that.setData({
    //   headimg: 'http://wolaimage-10029986.image.myqcloud.com/1-2016051421050035278.png',
    //   // uid: 1,
    //   nickname: '点击登陆',
    //   // roleImg: 3,
    //   showView1: true,
    //   showView2: true,
    //   showView3: true,
    //   showView4: true,
    //   showView5: true,
    //   showView6: true,
    //   showView7: true,
    //   showView8: true,
    //   showView9: true,
    //   showView10: true,
    // });
  },
  onGotUserInfo: function () {
    console.log(e.detail.errMsg)
    console.log(e.detail.userInfo)
    console.log(e.detail.rawData)
  },
  getUserInfoF: function () {

    var that = this;
    wx.getSetting({

      success: (res) => {
        console.info(res.authSetting);
        wx.login({
          success: function (res) {//登录成功
            if (res.code) {
              var code = res.code;
              wx.getUserInfo({
                success: res => {
                  app.globalData.userInfo = res.userInfo
                  console.info("一开始同意授权nickName===" + res.userInfo.nickName);
                  var encryptedData = encodeURIComponent(res.encryptedData);//一定要把加密串转成URI编码
                  var iv = res.iv;
                  // var code = res.code;
                  console.info("一开始同意授权code===" + code);
                  console.info("一开始同意授权encryptedData===" + encryptedData);
                  console.info("一开始同意授权iv===" + iv);
                  if (this.userInfoReadyCallback) {
                    this.userInfoReadyCallback(res)
                  }
                  post("/user/login", { iv: iv, code: code, encrypteddata: encryptedData }).then(data => {
                    console.log('登录返回数据：' + JSON.stringify(data))
                    try {
                      // 同步接口立即写入
                      wx.setStorageSync('login', data)
                      console.log('写入value2成功')
                    } catch (e) {
                      console.log('写入value2发生错误')
                    }
                    var role = "/resources/pt_icon.png";
                    if (data.data.role == "1") {
                      role = "/resources/pt_icon.png";
                      that.setData({
                        showView5: true,
                        showView6: true,
                        showView7: true,
                        showView8: true,
                        showView9: true,
                        showView10: true,
                      })
                    } else if (data.data.role == "2") {
                      role = "/resources/yr_icon.png";
                      that.setData({
                        showView1: true,
                        showView2: true,
                        showView3: true,
                        showView4: true,////
                        showView5: true,
                        showView6: true,
                        showView7: true,
                        showView8: true,
                        showView9: true,
                        showView10: true,
                      })

                    } else if (data.data.role == "4") {
                      role = "/resources/jz_icon.png";
                      that.setData({
                        showView4: true,
                        showView5: true,
                        showView6: true,
                        showView7: true,
                        showView8: true,
                        showView9: true,
                        showView10: true,
                      })

                    }
                    that.setData({
                      isLogin: true,
                      headimg: data.data.headimg,
                      uid: data.data.uid,
                      nickname: data.data.nickname,
                      roleImg: role,
                    }); 
                  });

                },
                fail(err) {
                  console.info(err.errMsg);
                  wx.showModal({
                    title: '警告',
                    cancelText: '不授权',
                    confirmText: '授权',
                    confirmColor: '#37C31A',
                    content: '若不授权微信登录，则无法使用艺人和剧组功能；点击重新获取授权，则可重新使用；' +
                    '若点击不授权，将无法使用便捷服务。',

                    success: function (res) {
                      if (res.confirm) {
                        console.log('用户点击确定')
                        wx.openSetting({
                          success: (res) => {
                            if (res.authSetting['scope.userInfo']) {
                              wx.getUserInfo({
                                success: res => {
                                  app.globalData.userInfo = res.userInfo
                                  console.info("再次同意授权" + res.userInfo.nickName);
                                  if (that.userInfoReadyCallback) {
                                    that.userInfoReadyCallback(res)
                                  }
                                }
                              })
                            } else {
                              console.info("再次不允许");
                              wx.redirectTo({
                                url: 'mine',
                              })
                            }
                          }
                        });
                      } else if (res.cancel) {
                        console.log('弹出框用户点击取消')
                        wx.redirectTo({
                          url: 'mine',
                        })

                      }
                    }
                  })

                }

              })
            }
          }
        })


      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 弹出框蒙层截断touchmove事件
   */
  preventTouchMove: function () {
  },
   
  /**
   * 对话框取消按钮点击事件
   */
  onCancel: function () {
    this.hideModal();
  },
  chongzhi: function () {
    var that = this;
    var uid = that.data.uid;
    wx.navigateTo({
      url: '../pay/pay?uid=' + uid,
    })
  },
  /**
 * formId获取
 */
  submitInfo: function (e) {
    var that = this;
    console.log('表单submitInfo:==================', e);
    console.log('表单id:==================', e.detail.formId);

    post("/user/addFormid", { uid: that.data.uid, form_id: e.detail.formId }).then(data => {
      
    })
  },
  minedetail: function () {
    var that = this;
    var uid = that.data.uid;
    wx.navigateTo({
      url: '../minedetail/minedetail?uid=' + uid,
    })
  },
  shixixiaoxi: function () {
    var that = this;
    var uid = that.data.uid;
    wx.navigateTo({
      url: '../msg/msg?uid=' + uid,
    })
  },
  wodejuzu: function () {
    var that = this;
    var uid = that.data.uid;
    wx.navigateTo({
      url: '../minejuzu/minejuzu?uid=' + 1,
    })
  },
  testRenzheng: function () {
    var that = this;
    var uid = that.data.uid;
    console.log("用户跳转传值===" + uid);
    if (that.data.role == 2 || that.data.role == 4){
      wx.navigateTo({
        url: '../rzSuccess/rzSuccess?role=' + that.data.role,
      })
    }else{
      wx.navigateTo({
        url: '../woderenzheng/woderenzheng?uid=' + uid,
      })
    }
  },
  wodeshenqing: function () {
    var that = this;
    var uid = that.data.uid;
    console.log("用户跳转传值===" + uid);
    wx.navigateTo({
      url: '../wodeshenqing/wodeshenqing?uid=' + uid,
    })
  },
  myCollect: function () {
    var that = this;
    var uid = that.data.uid;
    console.log("用户跳转传值===" + uid);
    wx.navigateTo({
      url: '../collect/collect?uid=' + uid,
    })
  },
  tuijian: function () {
    var that = this;
    var uid = that.data.uid;
    console.log("用户跳转传值===" + uid);
    wx.navigateTo({
      url: '../tuijian/tuijian?uid=' + uid,
    })
  },
  /**
   * 艺人资料跳转
   */
  yirenZiliao: function () {
    var that = this;
    var uid = that.data.uid;
    console.log("用户跳转传值===" + uid);
    wx.navigateTo({
      url: '../edityirendetail/edityirendetail?uid=' + uid,
    })
  },
  qianbao: function () {
    var that = this;
    var uid = that.data.uid;
    console.log("用户跳转传值===" + uid);
    wx.navigateTo({
      url: '../money/money?uid=' + uid,
    })
  },
  /**
   * 对话框确认按钮点击事件
   */
  onConfirm: function () {
    this.hideModal();
  },
  

})

